#include "app.h"
#include <uis.h>
#include "../dbg/dbg.h"

// TODO: set previous state here
#define BACK_STATE			MC_STATE_FB


// -----------------------------------------------------------------------------------------
// Local constants


// -----------------------------------------------------------------------------------------
// Local functions

static UINT32 HandleYes( EVENT_STACK_T * ev_st, void * app );
static UINT32 HandleNo( EVENT_STACK_T * ev_st, void * app );


// -----------------------------------------------------------------------------------------
// Global handlers array

const EVENT_HANDLER_ENTRY_T bmc_state_handlers[] =
{
	{ EV_YES,					HandleYes },
	{ EV_NO,					HandleNo },

	{ STATE_HANDLERS_END,		NULL }
};

// -----------------------------------------------------------------------------------------
// Global enter/exit state functions

UINT32 BMConfirmStateEnter( EVENT_STACK_T * ev_st, void * app, ENTER_STATE_TYPE_T type )
{
	MOTOCMD_T *		papp = motocmd(app);
	SU_PORT_T		port = papp->apt.port;
	UIS_DIALOG_T	dialog;
	CONTENT_T		cnt;
	UINT32			status;
	WCHAR *			name;
	
	dbg("BMConfirmStateEnter: Enter, type = %d", type);
	if ( type != ENTER_STATE_ENTER )
		return RESULT_OK;
	
	// TODO: add your code here
	name = papp->flist.list[papp->flist.cur].name;
	status = UIS_MakeContentFromString( "s0Ns1Ns3q2s3", &cnt, LANG_ADD_BOOKMARK, LANG_FOR_, name, LANG_QUOTATION_MARK );
	dbg("BMConfirmStateEnter: UIS_MakeContentFromString DONE, status = %d", status);
	
	if ( status != RESULT_OK )
		return status;
	
	dialog = UIS_CreateConfirmation( &port, &cnt );
	dbg("BMConfirmStateEnter: UIS_CreateConfirmation DONE, dialog = %d", dialog);
	
	if ( dialog == 0 )
		return RESULT_FAIL;
	papp->apt.dialog = dialog;
	
	dbg("BMConfirmStateEnter: END", 0);
	return RESULT_OK;
}

UINT32 BMConfirmStateExit( EVENT_STACK_T * ev_st, void * app, EXIT_STATE_TYPE_T type )
{
	MOTOCMD_T *		papp = motocmd(app);
	UINT32			status;
	
	dbg("BMConfirmStateExit: Enter, type = %d", type);
	if ( type != EXIT_STATE_EXIT )
		return RESULT_OK;
	
	status = APP_UtilUISDialogDelete( &papp->apt.dialog );
	
	dbg("BMConfirmStateExit: END, status = %d", status);
	return status;
}


// -----------------------------------------------------------------------------------------
// Other local functions

UINT32 HandleYes( EVENT_STACK_T * ev_st, void * app )
{
	MOTOCMD_T *		papp = motocmd(app);
	UINT32			status;
	UINT32			idx;
	WCHAR			uri[FILEURI_MAX_LEN + 1];
	
	dbg("HandleYes: Enter", 0);
	APP_ConsumeEv(ev_st, app);
	
	// TODO: add your code here
	idx = papp->flist.cur;
	u_mkpath(uri, papp->cur_dir, papp->flist.list[idx].name, FALSE);
	status = BM_Append(&papp->bookmarks, uri, papp->flist.list[idx].attr );
	
	if ( status == RESULT_OK ) {
		status = ShowNotice( ev_st, app, LANG_ADDED, NOTICE_TYPE_OK, BACK_STATE );
	} else {
		status = ShowNotice( ev_st, app, LANG_ERROR, NOTICE_TYPE_FAIL, BACK_STATE );
		errsnd(1);
	}
	
	dbg("HandleYes: END, status = %d", status);
	return status;
}

UINT32 HandleNo( EVENT_STACK_T * ev_st, void * app )
{
	UINT32			status;
	
	dbg("HandleNo: Enter", 0);
	APP_ConsumeEv(ev_st, app);
	
	// TODO: add your code here
	
	status = APP_UtilChangeState( BACK_STATE, ev_st, app );
	
	dbg("HandleNo: END, status = %d", status);
	return status;
}


